!pr0
!lm12
!rm75
Generate Machine Code with Applesoft.......Bob Sander-Cederlof

Apparently nobody picked up my challenge at the end of the article about Charlie Putney's faster spiral screen clear program (August 1983 AAL, page 16).  I suggested someone write a program in Applesoft which would in turn construct a machine language screen clear.

Nobody else did it, so I did.  And whether you are interested in fancy ways to clear the screen or not, the techniques I used may be put to other uses.

The task of building a screen clear program can be divided into two parts.  First, generate the memory addresses of the 960 cells on the screen, in the order (or path) that the spiral shift will follow.  Second, using that table of addresses, generate the 959 pairs of LDA and STA instructions necessary to move the screen one position along the spiral path.  There is really a third part:  to generate the necessary prologue and postlogue instructions to make those 959 LDA-STA pairs be executed 960 times, and to clear the vacated byte at the tail end of the spiral path.

After trying various ways to understand the spiral path, I arrived at a table-driven approach.  I put the table into data statements (lines 3000-3110 below), and made a simple loop to generate the 960 addresses (lines 100-150).

You might notice that the twelve lines of data correspond very closely to the parameters on Charlie Putney's macro calls.  After I typed in the twelve lines, I noticed a definite pattern.  I could have used only the first line of data, and computed the others by a simple algorithm:  increment each value smaller than 13, and decrement each value 13 or larger.  Well, no program is ever finished....

Once the 960 addresses are stored in array A%(0) through A%(959), I proceed to generate machine language code.  Line 180 does it all, with the help of four simple subroutines.  Then line 190 rings the bell, and line 200 calls the machine language program just generated for a fast two-and-a-half second demonstration.

During the address array building process, I fill up the screen with the letters U, D, L, and R.  These show the direction (up, down, left, and right) which a given character will be shifted along the spiral path.  The directions are just the opposite from the order in which the letters are displayed, because I generate the address list backwards (from head to tail).

During the generation of the machine language program, which takes about two minutes, I toggle the tail end character between normal to inverse video.  This gives you something to watch for those lloooonnggg two minutes.

The generation process is broken into four parts, represented by four subroutines at 5000, 5100, 5200, and 5300.

GOSUB 5000 generates a four byte prologue, starting at memory address $2710, or 10000. The code looks like this:

       LDX /-960
       LDY #-960

Actually, not -960, but -960/S.  S gives a step size.  Sidestepping a little from the main discussion, let me tell you about S.

Don Lancaster called last week to talk about a few things with Bill, and passed on the results of his experiments with Charlie's program.  He noted that the video refresh rate is 60 times per second, and that a 7.5 second screen clear moves a little more than two steps for each frame time.  Therefore you don't really SEE each step.  Therefore the screen clear routine could move each character two steps ahead at a time with the same smooth effect on the screen, but clearing the screen in half the time.  Or three steps, clearing in one third the time.  The variable S in my program lets you experiment with the number of steps each character moves during each pass.  As listed, S=3, so the screen clears in 2.5 seconds.

GOSUB 5100 generates the requisite number of LDA-STA pairs to move the screen one step of size S along the spiral path.

GOSUB 5200 generates the instructions to clear the bytes at the tail end of the spiral.  If S=3, you will get:

       LDA #$A0        BLANK
       STA $636
       STA $635
       STA $634

GOSUB 5300 generates the end-of-loop code:
       
       INY
       BNE LP
       INX
       BNE LP
       RTS
  LP   JMP 10004

The screen need not necessarily be cleared to all blanks.  By changing the value POKEd in the second part of line 5210 you can fill with all stars, or all white, or whatever.

Another interesting option occurs to me.  Given a table in the A% array of all the screen addresses, in any arrangement that suits my fancy, I can clear the screen in 2.5 to 7.5 seconds by shifting the screen along that particular path.  It could be random, spiral, kaleidoscopic, or whatever.

There are so many other things I could explain about this little program, I hardly know where to stop.  I think I'll stop here, and leave the rest for your own rewarding investigation and analysis.
 
